package com.cuit.controller.book;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuit.mapper.Book.CollectMapper;
import com.cuit.mapper.BookMapper;
import com.cuit.mapper.UserMapper;
import com.cuit.pojo.Book;
import com.cuit.pojo.Collect;
import com.cuit.pojo.User;
import com.cuit.result.R;
import com.cuit.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.assertj.core.util.Lists;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@CrossOrigin
@Api(tags = "收藏管理")
@RestController
@RequestMapping("/collect")
public class CollectController {
        @Resource
        private CollectMapper collectMapper;
        @Resource
        private BookMapper bookMapper;
        @Resource
        private UserMapper userMapper;
        @Resource
        private FileService fileService;
        @GetMapping("/addCollect/{colIds}")
        @ApiOperation("添加收藏")
        public R addCollect(@PathVariable String colIds){
            String[] ids = colIds.split(",");
            Collect col=new Collect();
            col.setUserId(Integer.valueOf(ids[0]));
            col.setBookId(Integer.valueOf(ids[1]));
            col.setCreateTime(new Date());
            int insert = collectMapper.insert(col);
            if(insert==1){
                return  R.ok();
            }else {
                return  R.error();
            }
        }
    @GetMapping("/isCollect/{colIds}")
    @ApiOperation("判断是否被该用户收藏")
    public R isCollect(@PathVariable String colIds){
        String[] ids = colIds.split(",");
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",ids[0]).eq("bookId",ids[1]);
        Collect collect = collectMapper.selectOne(queryWrapper);
        if(collect==null){
            return  R.ok();
        }else {
            return  R.error();
        }
    }
    @GetMapping("/delCollect/{colIds}")
    @ApiOperation("删除用户收藏")
    public R delCollect(@PathVariable String colIds){
        String[] ids = colIds.split(",");

        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",ids[0]).eq("bookId",ids[1]);
        int delete = collectMapper.delete(queryWrapper);
        if(delete==1){
            return  R.ok();
        }else {
            return  R.error();
        }
    }
    @GetMapping("/countCollect/{uId}")
    @ApiOperation("计算用户收藏的条数/和用户信息")
    public R countCollect(@PathVariable Integer uId){
            //存放条数和用户信息
        List<Object> personlist = Lists.newArrayList();
            //查询用户信息
        User user = userMapper.selectById(uId);
        user.setAvatar(fileService.allPath(user.getAvatar()));
        //用户收藏条数
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",uId);
        int count = collectMapper.selectCount(queryWrapper);
        personlist.add(user);
        personlist.add(count);
      return R.ok().data("personList",personlist);
    }
    @GetMapping("/selectCollect/{uId}")
    @ApiOperation("该用户所有收藏商品")
    public R selectCollect(@PathVariable Integer uId){

        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",uId);
        List<Collect> collectList = collectMapper.selectList(queryWrapper);
        List<Integer> bookIds= Lists.newArrayList();
        collectList.forEach(collect -> {
            System.out.println(collect.getBookId());
            bookIds.add(collect.getBookId());
        });
        List<Book> books = bookMapper.selectBatchIds(bookIds);
        books.forEach(book ->{
            book.setBookimg(fileService.allPath(book.getBookimg()));
        });
        return R.ok().data("books",books);
    }
    @GetMapping("/countUsers/{bId}")
    @ApiOperation("该商品有几个用户收藏")
    public R countUsers(@PathVariable Integer bId){
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bookId",bId);
        int count = collectMapper.selectCount(queryWrapper);
        return R.ok().data("countU",count);
    }
    @GetMapping("/DeleteManeyCol/{colIds}")
    @ApiOperation("批量删除用户收藏")
    public R delCollectM(@PathVariable String colIds){
        String[] cIds = colIds.split(",");
        String[] ids = cIds[1].split("-");
        for(int i=0;i<ids.length;i++){
            QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("userId",cIds[0]).eq("bookId",ids[i]);
            collectMapper.delete(queryWrapper);
        }
        //返回取消收藏后的商品
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",cIds[0]);
        List<Collect> collectList = collectMapper.selectList(queryWrapper);
        List<Integer> bookIds= Lists.newArrayList();
        collectList.forEach(collect -> {
            System.out.println(collect.getBookId());
            bookIds.add(collect.getBookId());
        });
        List<Book> books = bookMapper.selectBatchIds(bookIds);
        books.forEach(book ->{
            book.setBookimg(fileService.allPath(book.getBookimg()));
        });
        return R.ok().data("books",books);
    }
}
